<?php

namespace App\Exceptions;

use App\Contract\ModelExpose;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Throwable;


class Handler extends ExceptionHandler
{
    /**
     * The list of the inputs that are never flashed to the session on validation exceptions.
     *
     * @var array<int, string>
     */
    protected $dontFlash = [
        'current_password',
        'password',
        'password_confirmation',
    ];

    /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->reportable(function (Throwable $e) {
            //
        });
    }

    public function render($request,Throwable $exception)
    {
        if (
            $exception instanceof AuthenticationException
            ||
            $exception instanceof UnauthorizedHttpException
            ||
            $exception instanceof ModelNotFoundException
            ||
            $exception instanceof ThrottleRequestsException
            ||
            $exception instanceof GeneralException
            ||
            $exception instanceof ValidationException
            ||
            $exception instanceof NotFoundHttpException
            ||
            $exception instanceof MethodNotAllowedHttpException
        ) {
            /**
             * 主动抛出的异常
             */
            if ($exception instanceof GeneralException) {
                return error($exception->getMessage(),$exception->getCode(),$exception->getCode(),[]);
            }

            /**
             * 模型404异常
             */
            if ($exception instanceof ModelNotFoundException) {
                $name = $this->getModelExposeName($exception);
                return notFound('找不到对应的'.$name,400,400);
            }

            /**
             * 路由404异常
             */
            if ($exception instanceof NotFoundHttpException) {
                return notFound('路由不存在',400,400);
            }

            /**
             * 认证异常
             */
            if ($exception instanceof AuthenticationException) {
                return unAuthorized('认证失败',401,401);
            }

            /**
             * token失效异常
             */
            if ($exception instanceof UnauthorizedHttpException) {
                return unAuthorized('认证失败',401,401);
            }

            /**
             * 请求超频异常
             */
            if($exception instanceof ThrottleRequestsException) {
                return error('请求次数太频繁',400,400);
            }

            /**
             * 参数错误异常
             */
            if($exception instanceof ValidationException){
                return error(Arr::first(Arr::collapse($exception->errors())),422,422);
            }


            if($exception instanceof MethodNotAllowedHttpException){
                return error('请求方法不允许，请确认允许的请求方法(POST、GET、PUT、PATCH、DELETE)',416,416);
            }
        }else{
            return parent::render($request, $exception);
        }
    }

    public function getModelExposeName(ModelNotFoundException $exception)
    {
        $model = $exception->getModel();
        return ModelExpose::ExposeName[$model]??"数据";
    }
}
